﻿using System.Threading;
using System.Collections.Generic;

namespace Framework
{
    public class ConcurrentQueue<T>
    {
        private Mutex m_mutex;
        private Queue<T> m_queue;

        public ConcurrentQueue()
        {
            m_mutex = new Mutex();
            m_queue = new Queue<T>();
        }

        ~ConcurrentQueue()
        {
            m_mutex.Close();
        }

        public void Enqueue(T item)
        {
            m_mutex.WaitOne();
            m_queue.Enqueue(item);
            m_mutex.ReleaseMutex();
        }

        public T Dequeue()
        {
            m_mutex.WaitOne();
            T item = default(T);
            if (m_queue.Count > 0)
                item= m_queue.Dequeue();
            m_mutex.ReleaseMutex();
            return item;
        }
    }

}
